| Conditions | 1 |
| Paths | 2 |
| Total Lines | 292 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 42 | initialize: function () { |
||
| 43 | |||
| 44 | var self = this; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * API function to create a new Circle. |
||
| 49 | * |
||
| 50 | * @param type |
||
| 51 | * @param name |
||
| 52 | * @param callback |
||
| 53 | */ |
||
| 54 | this.createCircle = function (type, name, callback) { |
||
| 55 | |||
| 56 | var result = {status: -1}; |
||
| 57 | $.ajax({ |
||
| 58 | method: 'PUT', |
||
| 59 | url: OC.generateUrl('/apps/circles/v1/circles'), |
||
| 60 | data: { |
||
| 61 | type: type, |
||
| 62 | name: name |
||
| 63 | } |
||
| 64 | }).done(function (res) { |
||
| 65 | self.onCallback(callback, res); |
||
| 66 | }).fail(function () { |
||
| 67 | self.onCallback(callback, result); |
||
| 68 | }); |
||
| 69 | }; |
||
| 70 | |||
| 71 | |||
| 72 | this.listCircles = function (type, name, level, callback) { |
||
| 73 | var result = {status: -1}; |
||
| 74 | $.ajax({ |
||
| 75 | method: 'GET', |
||
| 76 | url: OC.generateUrl('/apps/circles/v1/circles'), |
||
| 77 | data: { |
||
| 78 | type: type, |
||
| 79 | name: name, |
||
| 80 | level: level |
||
| 81 | } |
||
| 82 | }).done(function (res) { |
||
| 83 | self.onCallback(callback, res); |
||
| 84 | }).fail(function () { |
||
| 85 | self.onCallback(callback, result); |
||
| 86 | }); |
||
| 87 | }; |
||
| 88 | |||
| 89 | |||
| 90 | this.detailsCircle = function (circleId, callback) { |
||
| 91 | var result = {status: -1}; |
||
| 92 | $.ajax({ |
||
| 93 | method: 'GET', |
||
| 94 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId) |
||
| 95 | }).done(function (res) { |
||
| 96 | self.onCallback(callback, res); |
||
| 97 | }).fail(function () { |
||
| 98 | self.onCallback(callback, result); |
||
| 99 | }); |
||
| 100 | }; |
||
| 101 | |||
| 102 | |||
| 103 | this.addMember = function (circleId, userId, callback) { |
||
| 104 | var result = {status: -1}; |
||
| 105 | $.ajax({ |
||
| 106 | method: 'PUT', |
||
| 107 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'), |
||
| 108 | data: { |
||
| 109 | name: userId |
||
| 110 | } |
||
| 111 | }).done(function (res) { |
||
| 112 | self.onCallback(callback, res); |
||
| 113 | }).fail(function () { |
||
| 114 | self.onCallback(callback, result); |
||
| 115 | }); |
||
| 116 | }; |
||
| 117 | |||
| 118 | |||
| 119 | this.addGroupMembers = function (circleId, groupId, callback) { |
||
| 120 | var result = {status: -1}; |
||
| 121 | $.ajax({ |
||
| 122 | method: 'PUT', |
||
| 123 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/groupmembers'), |
||
| 124 | data: { |
||
| 125 | name: groupId |
||
| 126 | } |
||
| 127 | }).done(function (res) { |
||
| 128 | self.onCallback(callback, res); |
||
| 129 | }).fail(function () { |
||
| 130 | self.onCallback(callback, result); |
||
| 131 | }); |
||
| 132 | }; |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | this.removeMember = function (circleId, userId, callback) { |
||
| 137 | var result = {status: -1}; |
||
| 138 | $.ajax({ |
||
| 139 | method: 'DELETE', |
||
| 140 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'), |
||
| 141 | data: { |
||
| 142 | member: userId |
||
| 143 | } |
||
| 144 | }).done(function (res) { |
||
| 145 | self.onCallback(callback, res); |
||
| 146 | }).fail(function () { |
||
| 147 | self.onCallback(callback, result); |
||
| 148 | }); |
||
| 149 | }; |
||
| 150 | |||
| 151 | |||
| 152 | this.levelMember = function (circleId, member, level, callback) { |
||
| 153 | var result = {status: -1}; |
||
| 154 | $.ajax({ |
||
| 155 | method: 'POST', |
||
| 156 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/level'), |
||
| 157 | data: { |
||
| 158 | member: member, |
||
| 159 | level: level |
||
| 160 | } |
||
| 161 | }).done(function (res) { |
||
| 162 | self.onCallback(callback, res); |
||
| 163 | }).fail(function () { |
||
| 164 | self.onCallback(callback, result); |
||
| 165 | }); |
||
| 166 | }; |
||
| 167 | |||
| 168 | |||
| 169 | this.linkGroup = function (circleId, groupId, callback) { |
||
| 170 | var result = {status: -1}; |
||
| 171 | $.ajax({ |
||
| 172 | method: 'PUT', |
||
| 173 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/groups'), |
||
| 174 | data: { |
||
| 175 | name: groupId |
||
| 176 | } |
||
| 177 | }).done(function (res) { |
||
| 178 | self.onCallback(callback, res); |
||
| 179 | }).fail(function () { |
||
| 180 | self.onCallback(callback, result); |
||
| 181 | }); |
||
| 182 | }; |
||
| 183 | |||
| 184 | |||
| 185 | this.unlinkGroup = function (circleId, groupId, callback) { |
||
| 186 | var result = {status: -1}; |
||
| 187 | $.ajax({ |
||
| 188 | method: 'DELETE', |
||
| 189 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/groups'), |
||
| 190 | data: { |
||
| 191 | group: groupId |
||
| 192 | } |
||
| 193 | }).done(function (res) { |
||
| 194 | self.onCallback(callback, res); |
||
| 195 | }).fail(function () { |
||
| 196 | self.onCallback(callback, result); |
||
| 197 | }); |
||
| 198 | }; |
||
| 199 | |||
| 200 | |||
| 201 | this.levelGroup = function (circleId, group, level, callback) { |
||
| 202 | var result = {status: -1}; |
||
| 203 | $.ajax({ |
||
| 204 | method: 'POST', |
||
| 205 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/group/level'), |
||
| 206 | data: { |
||
| 207 | group: group, |
||
| 208 | level: level |
||
| 209 | } |
||
| 210 | }).done(function (res) { |
||
| 211 | self.onCallback(callback, res); |
||
| 212 | }).fail(function () { |
||
| 213 | self.onCallback(callback, result); |
||
| 214 | }); |
||
| 215 | }; |
||
| 216 | |||
| 217 | |||
| 218 | this.joinCircle = function (circleId, callback) { |
||
| 219 | var result = {status: -1}; |
||
| 220 | $.ajax({ |
||
| 221 | method: 'GET', |
||
| 222 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/join'), |
||
| 223 | data: {} |
||
| 224 | }).done(function (res) { |
||
| 225 | self.onCallback(callback, res); |
||
| 226 | }).fail(function () { |
||
| 227 | self.onCallback(callback, result); |
||
| 228 | }); |
||
| 229 | }; |
||
| 230 | |||
| 231 | |||
| 232 | this.settingsCircle = function (circleId, settings, callback) { |
||
| 233 | var result = {status: -1}; |
||
| 234 | $.ajax({ |
||
| 235 | method: 'POST', |
||
| 236 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/settings'), |
||
| 237 | data: {settings: settings} |
||
| 238 | }).done(function (res) { |
||
| 239 | self.onCallback(callback, res); |
||
| 240 | }).fail(function () { |
||
| 241 | self.onCallback(callback, result); |
||
| 242 | }); |
||
| 243 | }; |
||
| 244 | |||
| 245 | |||
| 246 | this.leaveCircle = function (circleId, callback) { |
||
| 247 | var result = {status: -1}; |
||
| 248 | $.ajax({ |
||
| 249 | method: 'GET', |
||
| 250 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/leave'), |
||
| 251 | data: {} |
||
| 252 | }).done(function (res) { |
||
| 253 | self.onCallback(callback, res); |
||
| 254 | }).fail(function () { |
||
| 255 | self.onCallback(callback, result); |
||
| 256 | }); |
||
| 257 | }; |
||
| 258 | |||
| 259 | |||
| 260 | this.destroyCircle = function (circleId, callback) { |
||
| 261 | var result = {status: -1}; |
||
| 262 | $.ajax({ |
||
| 263 | method: 'DELETE', |
||
| 264 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId), |
||
| 265 | data: {} |
||
| 266 | }).done(function (res) { |
||
| 267 | self.onCallback(callback, res); |
||
| 268 | }).fail(function () { |
||
| 269 | self.onCallback(callback, result); |
||
| 270 | }); |
||
| 271 | }; |
||
| 272 | |||
| 273 | |||
| 274 | this.shareToCircle = function (circleId, source, type, item, callback) { |
||
| 275 | var result = {status: -1}; |
||
| 276 | $.ajax({ |
||
| 277 | method: 'PUT', |
||
| 278 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/share'), |
||
| 279 | data: { |
||
| 280 | source: source, |
||
| 281 | type: type, |
||
| 282 | item: item |
||
| 283 | } |
||
| 284 | }).done(function (res) { |
||
| 285 | self.onCallback(callback, res); |
||
| 286 | }).fail(function () { |
||
| 287 | self.onCallback(callback, result); |
||
| 288 | }); |
||
| 289 | }; |
||
| 290 | |||
| 291 | |||
| 292 | this.linkCircle = function (circleId, remote, callback) { |
||
| 293 | var result = {status: -1}; |
||
| 294 | $.ajax({ |
||
| 295 | method: 'POST', |
||
| 296 | url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/link'), |
||
| 297 | data: { |
||
| 298 | remote: remote |
||
| 299 | } |
||
| 300 | }).done(function (res) { |
||
| 301 | self.onCallback(callback, res); |
||
| 302 | }).fail(function () { |
||
| 303 | self.onCallback(callback, result); |
||
| 304 | }); |
||
| 305 | }; |
||
| 306 | |||
| 307 | |||
| 308 | this.linkStatus = function (linkId, status, callback) { |
||
| 309 | var result = {status: -1}; |
||
| 310 | $.ajax({ |
||
| 311 | method: 'POST', |
||
| 312 | url: OC.generateUrl('/apps/circles/v1/link/' + linkId + '/status'), |
||
| 313 | data: { |
||
| 314 | status: status |
||
| 315 | } |
||
| 316 | }).done(function (res) { |
||
| 317 | self.onCallback(callback, res); |
||
| 318 | }).fail(function () { |
||
| 319 | self.onCallback(callback, result); |
||
| 320 | }); |
||
| 321 | }; |
||
| 322 | |||
| 323 | this.onCallback = function (callback, result) { |
||
| 324 | if (callback && (typeof callback === 'function')) { |
||
| 325 | if (typeof result === 'object') { |
||
| 326 | callback(result); |
||
| 327 | } else { |
||
| 328 | callback({status: -1}) |
||
|
|
|||
| 329 | } |
||
| 330 | } |
||
| 331 | }; |
||
| 332 | |||
| 333 | } |
||
| 334 | |||
| 343 |
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.
Further Readings: